import os
import random
import time

import requests
from lxml import etree


url = 'http://pic.netbian.com/'
header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0'
}


def get_html(url):
    response = requests.get(url, headers=header)
    response.encoding = response.apparent_encoding
    # print(response.text)
    tree = etree.HTML(response.text)
    return tree


tree = get_html(url)
tp_urls = tree.xpath('//ul[@class="clearfix"]/li/a/@href')
titles = tree.xpath('//ul[@class="clearfix"]/li/a/@title')
for tp, title in zip(tp_urls, titles):
    # 拼凑完整的url
    img_url = 'http://pic.netbian.com' + tp
    tree = get_html(img_url)
    img_ = tree.xpath('//a[@id="img"]/img/@src')[0]
    img_urls = 'http://pic.netbian.com' + img_
    print(img_urls)
    # exit()
    path = os.path.join('./彼岸img', title.replace(" ", "_").replace("*", "") + '.jpg')

    with open(path, 'wb') as f:
        f.write(requests.get(img_urls).content)
        print(title)
    time.sleep(random.randint(1, 3))
